//
//  ThemeManager.m
//  iOS
//
//  Created by LiuHe@MacMini on 14-7-11.
//  Copyright (c) 2014年 cloudyoo. All rights reserved.
//

#import "ThemeManager.h"
//主题方案
#define ThemeHeader @"ThemeResource"
#define ThemeConfigName @"config.plist"

static ThemeManager  *themeManager = nil;

@implementation ThemeManager

+(ThemeManager*)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!themeManager) {
            themeManager = [[self alloc] init];
        }
    });
    return themeManager;
}

+(id)allocWithZone:(NSZone *)zone{
    @synchronized(self){
        if (!themeManager) {
            themeManager = [super allocWithZone:zone];
        }
    }
    return themeManager;
}

-(id)init{
    @synchronized(self)
    {
        if (self = [super init])
        {
            [self initCurrentTheme];
        }
    }
    
    return self;
}
//初始化当前主题
-(void)initCurrentTheme{
    self.themeName = [Data dataForKey:app_config_currenttheme];
    if (self.themeName==nil) {//默认取第一个为主题
        self.themeName = app_themes[0];
    }
    self.themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",ThemeHeader,self.themeName]];
}

-(NSString *)currentThemeName{
    return self.themeName;
}


#pragma mark 更改当前主题
-(void)changeThemeName:(NSString *)themeName{
    if ([self.themeName isEqualToString:themeName]) {
        return;
    }
    //更新配置
    [Data setData:themeName forKey:app_config_currenttheme];
    self.themeName = themeName;
    self.themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",ThemeHeader,self.themeName]];
}

#pragma mark 下载主题包
-(void)downloadTheme:(NSString *)url{
    
    
}

#pragma mark 获得当前主题的图片路径
-(NSString *)getImagePath:(NSString *)imgName
{
    return [NSString stringWithFormat:@"%@/%@",self.themePath,imgName];
}

#pragma mark 获得当前主题的UIImage
-(UIImage *)getImage:(NSString *)imgName{
    NSString *path = [self getImagePath:imgName];
    return [UIImage imageWithContentsOfFile:path];
}

#pragma mark 获得当前主题的配置的key
-(UIColor *)colorForKey:(NSString *)key{
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[self.themePath stringByAppendingPathComponent:ThemeConfigName]];
    NSString *strColor = [dict valueForKey:key];
    if (strColor==nil) {
        return nil;
    }
    return [APPUtils colorWithHexString:strColor];
}

@end
